package com.cj.cms.web.security;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.cj.cms.entity.Channel;
import com.cj.cms.entity.Template;
import com.cj.cms.service.ChannelManager;
import com.cj.cms.service.TemplateManager;
import com.cj.cms.template.PublishChannel;
import com.cj.common.orm.Page;
import com.cj.common.orm.PropertyFilter;
import com.cj.common.orm.hibernate.EntityManager;
import com.cj.common.orm.hibernate.HibernateWebUtils;
import com.cj.common.web.struts2.CRUDActionSupport;
import com.cj.common.web.struts2.Struts2Utils;

public class ChannelAction extends CRUDActionSupport<Channel>{
	private final Long rootId = 1L;
	
	private Long parentId;
	@Autowired
	private ChannelManager channelManager;
	@Autowired
	private PublishChannel publishChannel;

	@Autowired
	private TemplateManager templateManager;

	private List<Template> templates;
	private List<Long> checkedOtherTemplateIdsList;
	@Override
	public EntityManager getEntityManager() {
		return channelManager;
	}
	
	public String publish(){
		try {
			List<Channel> channels  = channelManager.getAll();
			for(Channel channel :channels){
				publishChannel.publishChannel(channel);
			}
			Struts2Utils.renderText("true");
		} catch (Exception e) {
			setErrMessage(e.getMessage());
			Struts2Utils.renderText("error:" + getErrMessage());
		}
		return null;
	}
	@Override
	public String save() {
		try {
			channelManager.save(entity);
			addActionMessage("保存成功");
			publishChannel.publishChannel(entity);
			if(entity.getParent() != null && entity.getParent().getId() != null)
				parentId = entity.getParent().getId();
			return list();			
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}
	
	@Override
	public String input() {
		try {
			templates = templateManager.findByTypes(Template.OTHER_TEMPLATE);
			checkedOtherTemplateIdsList = entity.getOtherTemplateIdsList();
			return INPUT;
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}
	
	
	@Override
	public String list() {
		try {
			HttpServletRequest request = Struts2Utils.getRequest();
			List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(request);
			if(parentId != null)
				filters.add(new PropertyFilter("parent.id",parentId,PropertyFilter.MatchType.EQ));
			else
				filters.add(new PropertyFilter("parent.id",rootId,PropertyFilter.MatchType.EQ));
			page.setOrder(Page.ASC);
			page.setOrderBy("orderNum");
			page = getEntityManager().search(page, filters);
			return SUCCESS;
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}
	
	/**
	 * 左边带树的channel  list
	 * @return
	 */
	public String treeList(){
		try {
			return "treeList";
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}
	public String getTreeXml(){
		try {
			String xml;
			if (getId() == null) {
				xml = channelManager.getMenuXmlTree(null,rootId);
			} else {
				xml = channelManager.getMenuXmlTree(null,getId());
			}
			Struts2Utils.renderXml(xml);
			return null;
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}
	public void prepareSave() throws Exception {
		if (getId() != null) {
			entity = channelManager.load(getId());
			entity.setParent(null);
			entity.setTemplate(null);
			entity.setContentTemplate(null);
			entity.setPertainTemplate(null);
		} else {
			entity = new Channel();
		}
	}	
	protected void prepareModel() throws Exception{
		if (getId() != null) {
			entity = channelManager.load(getId());
		} else {
			entity = new Channel();
			if(parentId != null){
				entity.setParent(channelManager.load(parentId));
				entity.setArticleNum(25);
			}
		}
	}
	
	public Long getRootId() {
		return rootId;
	}
	public Long getParentId() {
		return parentId;
	}
	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}


	public List<Template> getTemplates() {
		return templates;
	}


	public void setTemplates(List<Template> templates) {
		this.templates = templates;
	}

	public List<Long> getCheckedOtherTemplateIdsList() {
		return checkedOtherTemplateIdsList;
	}

	public void setCheckedOtherTemplateIdsList(
			List<Long> checkedOtherTemplateIdsList) {
		this.checkedOtherTemplateIdsList = checkedOtherTemplateIdsList;
	}


}
